import { get, post } from '@/utils/request'

export default {
	// AI服务健康检查
	async checkAIHealth() {
		try {
			const res = await get('/api/ai/health')
			return res.data
		} catch (err) {
			console.error('AI健康检查失败:', err)
			throw err
		}
	},

	// 获取AI服务信息
	async getAIServices() {
		try {
			const res = await get('/api/ai/services')
			return res.data
		} catch (err) {
			console.error('获取AI服务信息失败:', err)
			throw err
		}
	},

	// AI聊天接口
	async chatWithAI(messages, provider = null) {
		try {
			const data = { messages }
			if (provider) {
				data.provider = provider
			}
			const res = await post('/api/ai/chat', data)
			return res.data
		} catch (err) {
			console.error('AI聊天失败:', err)
			throw err
		}
	},

	// AI文本生成
	async generateText(prompt, provider = null) {
		try {
			const data = { prompt }
			if (provider) {
				data.provider = provider
			}
			const res = await post('/api/ai/generate', data)
			return res.data
		} catch (err) {
			console.error('AI文本生成失败:', err)
			throw err
		}
	},

	// 流式AI聊天
	async chatWithAIStream(messages, onMessage, provider = null) {
		try {
			const data = { messages }
			if (provider) {
				data.provider = provider
			}
			const res = await post('/api/ai/chat/stream', data)
			return res.data
		} catch (err) {
			console.error('AI流式聊天失败:', err)
			throw err
		}
	},

	// 商品描述生成
	async generateGoodsDescription(goodsData) {
		try {
			const data = {
				title: goodsData.title || '',
				category: goodsData.category || '商品',
				condition: goodsData.condition || '二手',
				price: goodsData.price || 0,
				originalPrice: goodsData.originalPrice || 0,
				brand: goodsData.brand || '',
				model: goodsData.model || '',
				usage: goodsData.usage || '',
				features: goodsData.features || [],
				provider: 'huoshan'
			}
			const res = await post('/api/ai/generate/goods-description', data)
			return res.data
		} catch (err) {
			console.error('AI商品描述生成失败:', err)
			throw err
		}
	},

	// 价格建议（转转商城专用）
	async suggestPrice(goodsData) {
		try {
			const data = {
				title: goodsData.title || '',
				category: goodsData.category || '商品',
				condition: goodsData.condition || '二手',
				originalPrice: goodsData.originalPrice || 0,
				brand: goodsData.brand || '',
				model: goodsData.model || '',
				usage: goodsData.usage || '',
				features: goodsData.features || [],
				marketPrice: goodsData.marketPrice || 0
			}
			const res = await post('/api/ai/suggest/price', data)
			return res.data
		} catch (err) {
			console.error('AI价格建议失败:', err)
			throw err
		}
	},

	// 商品标题优化
	async optimizeGoodsTitle(title, category) {
		try {
			const data = {
				title,
				category: category || '商品'
			}
			const res = await post('/api/ai/optimize/title', data)
			return res.data
		} catch (err) {
			console.error('AI商品标题优化失败:', err)
			throw err
		}
	},

	// 商品规格建议
	async suggestGoodsSpecs(category, description) {
		try {
			const data = {
				category: category || '商品',
				description: description || ''
			}
			const res = await post('/api/ai/suggest/specs', data)
			return res.data
		} catch (err) {
			console.error('AI商品规格建议失败:', err)
			throw err
		}
	},

	// 商品图片描述生成
	async generateImageDescription(imageUrl, goodsInfo) {
		try {
			const data = {
				imageUrl,
				goodsInfo: goodsInfo || {}
			}
			const res = await post('/api/ai/generate/image-description', data)
			return res.data
		} catch (err) {
			console.error('AI图片描述生成失败:', err)
			throw err
		}
	},

	// 商品分类建议
	async suggestCategory(description, title) {
		try {
			const data = {
				description: description || '',
				title: title || ''
			}
			const res = await post('/api/ai/suggest/category', data)
			return res.data
		} catch (err) {
			console.error('AI商品分类建议失败:', err)
			throw err
		}
	},

	// 商品发布建议（综合建议）
	async suggestGoodsPublish(goodsData) {
		try {
			const data = {
				title: goodsData.title || '',
				description: goodsData.description || '',
				category: goodsData.category || '商品',
				price: goodsData.price || 0,
				originalPrice: goodsData.originalPrice || 0,
				images: goodsData.images || [],
				condition: goodsData.condition || '二手',
				brand: goodsData.brand || '',
				model: goodsData.model || ''
			}
			const res = await post('/api/ai/suggest/publish', data)
			return res.data
		} catch (err) {
			console.error('AI商品发布建议失败:', err)
			throw err
		}
	}
}